<?php

namespace App\Listeners;

use App\Events\ActivityPaused;
use App\Models\Activity\Activity;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;

class FlushRedisStorage
{
    protected $vote;

    /**
     * Create the event listener.
     */
    public function __construct()
    {
    }

    /**
     * Handle the event.
     *
     * @param  ActivityPaused  $event
     * @return void
     */
    public function handle($event)
    {
        $this->flush($event->activityId);
    }

    public function flush($id)
    {
        $redis = \app('redis');

        $r1 = $r2 = [];
        $allowDeleteKeys = ['vote:', 'rule:', 'act:', 'items:', 'rules:', 'ids:'];

        // 删除投票本身原数据, 除了ranking都删除
        foreach ($allowDeleteKeys as $key) {
            $r1[] = $redis->del($key . $id);
        }

        // 获取到所有投票项的key, item::和_item::
        $itemKeys = $redis->keys('*item:' . $id . ':*');

        // 删除投票项
        foreach ($itemKeys as $item) {
            $r2[] = $redis->del($item);
        }

        return $r1 == $r2;
    }
}
